/** * 基础菜单 商品管理 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>小程序用户</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.deptName"
          placeholder="输入部门名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input
          size="small"
          v-model="formInline.deptNo"
          placeholder="输入部门代码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      @selection-change="handleSelectionChange"
      element-loading-text="拼命加载中"
      style="width: 100%;"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="img_id" label="ID" width="120">
      </el-table-column>
      <el-table-column sortable prop="nickname" label="用户昵称" width="220">
      </el-table-column>
      <el-table-column sortable label="用户头像" width="180">
        <template slot-scope="scope">
          <img
            :src="scope.row.url"
            @click="tply(scope.row.url)"
            width="40"
            height="40"
            class="head_pic"
          />
        </template>
      </el-table-column>
      <el-table-column sortable prop="openid" label="用户openid" width="280">
      </el-table-column>
      <el-table-column sortable prop="zctime" label="注册时间" width="300">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteUser(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 图片预览 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <img :src="url" alt="" style="width: 100%;" />

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="30%"
      @click="closeDialog"
    >
      <div
        style="display: flex;flex-direction: row;justify-content: space-around;"
      >
        <img :src="url" style="width: 160px;height: auto;" alt="" />
        <div>
          <div>上传时间：{{ release_time }}</div>
          <div>编号ID：{{ img_id }}</div>
          <div>
            状态：<span style="font-weight: bold;">{{ tag }}</span>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="tg(img_id)" type="success" plain
          >通过</el-button
        >
        <el-button size="small" @click="btg(img_id)" type="danger" plain
          >不通过</el-button
        >
        <el-button size="small" @click="closeDialog">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { nice } from "echarts/lib/util/number";
import { deptList, deptSave, deptDelete } from "../../api/userMG";
import Pagination from "../../components/Pagination";
import axios from "axios";
import { number } from "echarts/lib/export";
export default {
  data() {
    return {
      // 图片预览
      dialogVisible: false,
      // 定义不报错的变量
      url: "",
      nickname: "",
      tag: "",
      release_time: "",
      img_id: "",
      temp: "", //全选多选那块记录id的,用于一键审核通过,或者不审核通过
      // 分割线················
      imgArr: [],
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: "添加",
      editForm: {
        deptId: "",
        deptName: "",
        deptNo: "",
        token: localStorage.getItem("logintoken")
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: "请输入部门名称", trigger: "blur" }
        ],
        deptNo: [{ required: true, message: "请输入部门代码", trigger: "blur" }]
      },
      formInline: {
        page: 1,
        limit: 10,
        varLable: "",
        varName: "",
        token: localStorage.getItem("logintoken")
      },
      // 删除部门
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken")
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    };
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    // axios.get(`http://localhost:3000/api/getImg`).then(res => {
    //   console.log(res);
    // });
    this.getImgArr();
  },
  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 图片预览
    tply(url) {
      console.log(url);
      this.dialogVisible = true;
      if (url != undefined && url != "undefined") {
        this.title = "图片预览";
        this.url = url;
      } else {
      }
    },
    handleClose(done) {
      // 点击外面遮罩和右上角的关闭图片，关闭预览
      this.dialogVisible = false;
    },
    handleSelectionChange(val) {
      this.temp = "";
      // console.log(val);
      for (let i = 0; i < val.length; i++) {
        // 判断如果是最后一个则不需要在最后加逗号
        if (i == val.length - 1) {
          this.temp += `${val[i].img_id}`;
        } else {
          this.temp += `${val[i].img_id},`;
        }
      }
      // console.log(this.temp);
    },
    // 审核全部通过
    qbtg() {
      if (this.temp == "") {
        this.$message.error("请选中第一列勾选框再试一试");
        return;
      }
      axios
        .post(
          `http://localhost:3000/api/xg?id=${
            this.temp
          }&state=2&time=${this.getCurrentTime()}`
        )
        .then(res => {
          if (res.status == 200) {
            this.getImgArr();
            this.closeDialog();
          }
        });
    },
    // 一键删除
    allDel() {
      if (this.temp == "") {
        this.$message.error("请选中第一列勾选框再试一试");
        return;
      }
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          axios
            .post(`http://localhost:3000/api/delete?id=${this.temp}`)
            .then(res => {
              if (res.status == 200) {
                this.$message({
                  type: "success",
                  message: "已删除!"
                });
                this.getImgArr();
                this.closeDialog();
              }
            })
            .catch(err => {
              this.loading = false;
              this.$message.error("删除失败，请稍后再试！");
            });
          return;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 审核全部不通过b
    qbbtg() {
      if (this.temp == "") {
        this.$message.error("请选中第一列勾选框再试一试");
        return;
      }
      axios
        .post(
          `http://localhost:3000/api/xg?id=${
            this.temp
          }&state=3&time=${this.getCurrentTime()}`
        )
        .then(res => {
          if (res.status == 200) {
            this.getImgArr();
            this.closeDialog();
          }
        });
    },
    // 审核单个通过
    tg(id) {
      console.log(id);
      axios
        .post(
          `http://localhost:3000/api/xg?id=${id}&state=2&time=${this.getCurrentTime()}`
        )
        .then(res => {
          if (res.status == 200) {
            this.getImgArr();
            this.closeDialog();
          }
        });
    },
    // 审核单个不通过
    btg(id) {
      axios
        .post(
          `http://localhost:3000/api/xg?id=${id}&state=3&time=${this.getCurrentTime()}`
        )
        .then(res => {
          if (res.status == 200) {
            this.getImgArr();
            this.closeDialog();
          }
        });
    },

    // 获取图片数据
    getImgArr() {
      // console.log(this.getCurrentTime());
      this.imgArr = [];
      axios.get(`http://localhost:3000/api/user`).then(res => {
        console.log(res);
        for (let i = 0; i < res.data.list.length; i++) {
          this.imgArr.push({
            img_id: res.data.list[i].id,
            openid: res.data.list[i].openid,
            zctime: res.data.list[i].zctime,
            img_number: res.data.list[i].img_number,
            url: `http://localhost:3000/${res.data.list[i].user_img}`,
            nickname: res.data.list[i].nickname
          });
        }
        // console.log(this.imgArr);
        return (this.listData = this.imgArr);
      });
    },
    filterTag(value, row) {
      return row.tag === value;
    },

    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true;
      // console.log(index);
      // console.log(row);
      if (row != undefined && row != "undefined") {
        this.title = "壁纸审核";
        this.url = row.url;
        this.release_time = row.release_time;
        this.tag = row.tag;
        this.img_id = row.img_id;
        this.editForm.deptId = row.deptId;
        this.editForm.deptName = row.deptName;
        this.editForm.deptNo = row.deptNo;
      } else {
        this.title = "添加";
        this.editForm.deptId = "";
        this.editForm.deptName = "";
        this.editForm.deptNo = "";
      }
    },

    // 删除单个信息
    deleteUser(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log(row.img_id);
          axios
            .post(`http://localhost:3000/api/delete?id=${row.img_id}`)
            .then(res => {
              console.log(res);
              this.$message({
                type: "success",
                message: "已删除!"
              });
              this.getImgArr();
            })
            .catch(err => {
              this.loading = false;
              this.$message.error("删除失败，请稍后再试！");
            });
          return;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
    // 获取当前时间
    getCurrentTime() {
      var now = new Date();
      var year = now.getFullYear();
      var month = now.getMonth() + 1; // 注意，月份从0开始，所以需要加1
      var day = now.getDate();
      var hours = now.getHours();
      var minutes = now.getMinutes();
      var seconds = now.getSeconds();

      // 为了保证格式一致，这里进行了一些格式化处理
      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      hours = hours < 10 ? "0" + hours : hours;
      minutes = minutes < 10 ? "0" + minutes : minutes;
      seconds = seconds < 10 ? "0" + seconds : seconds;

      // 拼接输出
      var timeStr =
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hours +
        ":" +
        minutes +
        ":" +
        seconds;

      return timeStr;
    }
  }
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>
